package com.pet.platform.controller;

import com.pet.platform.common.Result;
import com.pet.platform.dto.AddressDTO;
import com.pet.platform.service.AddressService;
import com.pet.platform.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址管理控制器
 */
@RestController
@RequestMapping("/api/user")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 获取用户地址列表
     *
     * @return 地址列表
     */
    @GetMapping("/addresses")
    public Result<List<AddressDTO>> getAddresses() {
        Long userId = SecurityUtil.getCurrentUserId();
        List<AddressDTO> addressList = addressService.getAddressList(userId);
        return Result.success(addressList);
    }

    /**
     * 创建新地址
     *
     * @param addressDTO 地址信息
     * @return 创建后的地址
     */
    @PostMapping("/addresses")
    public Result<AddressDTO> createAddress(@RequestBody @Validated AddressDTO addressDTO) {
        Long userId = SecurityUtil.getCurrentUserId();
        AddressDTO createdAddress = addressService.createAddress(userId, addressDTO);
        return Result.success(createdAddress);
    }

    /**
     * 更新地址
     *
     * @param id 地址ID
     * @param addressDTO 地址信息
     * @return 更新后的地址
     */
    @PutMapping("/addresses/{id}")
    public Result<AddressDTO> updateAddress(@PathVariable Long id, @RequestBody @Validated AddressDTO addressDTO) {
        Long userId = SecurityUtil.getCurrentUserId();
        AddressDTO updatedAddress = addressService.updateAddress(userId, id, addressDTO);
        return Result.success(updatedAddress);
    }

    /**
     * 删除地址
     *
     * @param id 地址ID
     * @return 操作结果
     */
    @DeleteMapping("/addresses/{id}")
    public Result<Boolean> deleteAddress(@PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        addressService.deleteAddress(userId, id);
        return Result.success(true);
    }

    /**
     * 获取地址详情
     *
     * @param id 地址ID
     * @return 地址详情
     */
    @GetMapping("/addresses/{id}")
    public Result<AddressDTO> getAddressDetail(@PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        AddressDTO addressDTO = addressService.getAddressDetail(userId, id);
        return Result.success(addressDTO);
    }
} 